package icu.qx.sys.contorller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import icu.qx.sys.domain.SysCode;
import icu.qx.sys.param.SysCodeParam;
import icu.qx.sys.service.SysCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.web.controller.BaseController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RequestMapping("/sys/code")
@RestController
public class SysCodeController extends BaseController {
    @Autowired
    private SysCodeService sysCodeService;

    /**
     * 根据条件查询
     */
    @RequiresPermissions("system:code:list")
    @GetMapping("/list")
    public TableDataInfo sysCodeList(SysCodeParam sysCodeParam) {
        startPage();
        List<SysCode> sysCodes = sysCodeService.selectSysCodeList(sysCodeParam);
        return getDataTable(sysCodes);
    }

    /**
     * 通过主键id查询详细信息
     */
    @RequiresPermissions("system:code:query")
    @GetMapping(value = "/{codeId}")
    public R<SysCode> getInfo(@PathVariable("codeId") Long codeId) {
        return R.ok(sysCodeService.selectSysCodeById(codeId));
    }

    /**
     * 通过关联表单查询详细信息
     */
    @GetMapping
    public R<SysCode> getInfoByCodeTableName(@RequestParam("codeTableName") String codeTableName) {
        return R.ok(sysCodeService.selectSysCodeByCodeTableName(codeTableName));
    }

    /**
     * 新增系统编码
     */
    @RequiresPermissions("system:code:add")
    @PostMapping
    public R<Integer> add(@Validated @RequestBody SysCode sysCode) {
        if (!sysCodeService.checkShowNameUnique(sysCode)) {
            return R.fail("新增编码'" + sysCode.getCodeShowName() + "'失败，编码名称已存在");
        }
        return R.ok(sysCodeService.insertSysCode(sysCode));
    }

    /**
     * 修改系统编码
     */
    @PutMapping
    public R<Integer> edit(@Validated @RequestBody SysCode sysCode) {
        if (!sysCodeService.checkShowNameUnique(sysCode)) {
            return R.fail("修改编码'" + sysCode.getCodeShowName() + "'失败，编码名称已存在");
        }
        return R.ok(sysCodeService.updateSysCode(sysCode));
    }

    /**
     * 修改系统编码
     */
    @PutMapping("/currNum")
    public R<Integer> editSysCodeCurrNum(@Validated @RequestBody SysCode sysCode) {
        if (!sysCodeService.checkShowNameUnique(sysCode)) {
            return R.fail("修改编码'" + sysCode.getCodeShowName() + "'失败，编码名称已存在");
        }
        return R.ok(sysCodeService.updateSysCode(sysCode));
    }

    /**
     * 导出
     */
    @RequiresPermissions("system:code:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysCodeParam sysCodeParam) {
        List<SysCode> list = sysCodeService.selectSysCodeList(sysCodeParam);
        ExcelUtil<SysCode> util = new ExcelUtil<SysCode>(SysCode.class);
        util.exportExcel(response, list, "编码规则");
    }
}
